//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "motor.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)


void Motor::draw(){

        glColor3f(0.3,0.3,0.3);
        glLineWidth(3);
        glBegin(GL_LINES);
               glVertex2f(satelite->getX(),
                          satelite->getY());
               glVertex2f(base->centro->getX(),
                          base->centro->getY());
        glEnd();

        base->draw();

        glBegin(GL_TRIANGLE_FAN);
        glColor3f(0.5,0.5,0.5);
        for(int i=0;i<50;i++)

                if(reboto_antes[i])
                         glColor3f(1,1,1);

        glVertex2f(satelite->getX(),satelite->getY());
        glColor3f(0,0,0);

        for(int i=0;i<50;i++)
         if(reboto_antes[i])
                glColor3f(1,1,1);



        for(int i=0;i<=NITERC;i++){
                glVertex2f(satelite->getX()+rs*cos((2*M_PI*i)/NITERC),
                           satelite->getY()+rs*sin((2*M_PI*i)/NITERC));

        }
        glEnd();

}
bool Motor::invasion(PV * pos,PV * sen,GLdouble rad,PV * &normal){
        GLdouble distancia;
        PV * cc = new PV ();

        cc->vectorDosPV(pos,base->centro);
        distancia = cc->modulo();
        //base del motor
        if (distancia <= (rad+base->radio)){
                cc->vectorDosPV(pos,base->centro);
                distancia = cc->modulo();
                sen->normalizacion();
                sen->escalacion(distancia-base->radio-rad);
                normal->vectorDosPV(base->centro,pos);
                normal->normalizacion();
                delete cc;

                return true;
        }
        cc->vectorDosPV(pos,satelite);
        distancia = cc->modulo();

        //satelite
        if (distancia <= (rad+rs)){
                cc->vectorDosPV(pos,base->centro);
                distancia = cc->modulo();
                sen->normalizacion();
                sen->escalacion(distancia-rs-rad);
                normal->vectorDosPV(satelite,pos);
                normal->normalizacion();
                this->ang=-ang;
                delete cc;
                
                return true;
        }
        //segmento
        PV* sp=new PV(0,0,0);
        PV* cp=new PV(0,0,0);
        PV* sc=new PV(0,0,0);
        PV* cs=new PV(0,0,0);
        sc->vectorDosPV(satelite,base->centro);
        cs->vectorDosPV(base->centro,satelite);
        sp->vectorDosPV(satelite,pos);
        cp->vectorDosPV(base->centro,pos);


        GLdouble alfa=sp->angulo(sp,sc);
        GLdouble beta=sp->angulo(cp,cs);
        distancia=sp->modulo()*sin(alfa);
        GLdouble d0= sp->modulo()*cos(alfa);
        GLdouble d1= cp->modulo()*cos(beta);

        if(alfa < M_PI/2 && beta < M_PI/2 && d0+d1<=(sc->modulo()+0.1) && distancia <= rad){

                sc->normalizacion();
                sc->escalacion(d0);

                normal=sc->resta(sp,sc);

                normal->normalizacion();
                if(normal->angulo(normal,sen)<=0)
                        normal->escalacion(-1);
                delete cc;

                delete sc;
                delete cs;
                delete cp;
                delete sp;
                this->ang=-ang;
                return true;
        }

        delete sc;
                delete cs;
                delete cp;
                delete sp;
        delete cc;

        return false;
}
bool Motor::invasion2(PV * pos,PV * sen,GLdouble rad,PV * &normal){
        GLdouble distancia;
        PV * cc = new PV ();
        PV * des= pos->copiar();
        des->suma(pos,sen);
        cc->vectorDosPV(des,base->centro);
        distancia = cc->modulo();
        //base del motor
        if (distancia <= (rad+base->radio)){
                cc->vectorDosPV(pos,base->centro);
                distancia = cc->modulo();
                sen->normalizacion();
                sen->escalacion(distancia-base->radio-rad);
                normal->vectorDosPV(base->centro,des);
                normal->normalizacion();
                delete cc;
                delete des;
                return true;
        }
        cc->vectorDosPV(des,satelite);
        distancia = cc->modulo();

        //satelite
        if (distancia <= (rad+rs)){
                cc->vectorDosPV(pos,base->centro);
                distancia = cc->modulo();
                sen->normalizacion();
                sen->escalacion(distancia-rs-rad);
                normal->vectorDosPV(satelite,des);
                normal->normalizacion();
                this->ang=-ang;
                delete cc;
                delete des;
                return true;
        }
        //segmento
        PV* sp=new PV(0,0,0);
        PV* cp=new PV(0,0,0);
        PV* sc=new PV(0,0,0);
        PV* cs=new PV(0,0,0);
        sc->vectorDosPV(satelite,base->centro);
        cs->vectorDosPV(base->centro,satelite);
        sp->vectorDosPV(satelite,des);
        cp->vectorDosPV(base->centro,des);


        

        //distancia = cc->modulo();
        /*
        GLdouble a0=(cc->prodEscalar(cc,pms))/(cc->prodEscalar(pms,pms));
        if(a0<0) a0=-a0;
        distancia=a0*pms->modulo();
        GLdouble b0 = (s->prodEscalar(cc,s))/(s->prodEscalar(s,s));
        GLdouble d1= b0*s->modulo();   */

        GLdouble alfa=sp->angulo(sp,sc);
        GLdouble beta=sp->angulo(cp,cs);
        distancia=sp->modulo()*sin(alfa);
        GLdouble d0= sp->modulo()*cos(alfa);
        GLdouble d1= cp->modulo()*cos(beta);
        GLdouble a =sc->modulo();
           if(distancia<=rad)
                distancia=distancia;
        //if((distancia>0 && distancia <= rad)||(distancia<0 && distancia >= rad) ){
         if(alfa < M_PI/2 && beta < M_PI/2 && d0+d1<=(sc->modulo()+0.1) && distancia <= rad){

                sc->normalizacion();
                sc->escalacion(d0);

                normal=sc->resta(sp,sc);

                /*normal=sc->copiar();
                normal->calculaNormal();*/
                normal->normalizacion();
                if(normal->angulo(normal,sen)<=0)
                        normal->escalacion(-1);
                delete cc;
                delete des;
                delete sc;
                delete cs;
                delete cp;
                delete sp;
                return true;
        }

        delete sc;
                delete cs;
                delete cp;
                delete sp;
        delete cc;
        delete des;
        return false;
}
